// 1. 定义接口
interface IVisitor {
    visit(e: Element): void;
  }
  
  // 2. 定义具体访问者
  class ConcreteVisitor implements IVisitor {
    visit(e: Element): void {
      console.log("具体访问者访问" + e.operation());
    }
  }
  
  // 3. 定义元素
  interface Element {
    operation(): string;
  }
  
  // 4. 定义具体元素
  class ConcreteElementA implements Element {
    operation(): string {
      return "具体元素A的操作。";
    }
  }
  
  class ConcreteElementB implements Element {
    operation(): string {
      return "具体元素B的操作。";
    }
  }
  
  // 5. 定义对象结构
  class ObjectStructure {
    private list: Element[] = [];
    add(e: Element): void {
      this.list.push(e);
    }
    remove(e: Element): void {
      this.list = this.list.filter((item) => item !== e);
    }
    accept(visitor: IVisitor): void {
      this.list.forEach((item) => {
        item.operation();
      });
    }
  }
  
  // 6. 客户端代码
  const os = new ObjectStructure();
  os.add(new ConcreteElementA());
  os.add(new ConcreteElementB());
  
  const visitor = new ConcreteVisitor();
  os.accept(visitor);